//
//  HomeSegmentV.m
//  ShandsMod
//
//  Created by zh_s on 2023/3/18.
//  Copyright © 2023 zh_s. All rights reserved.
//

#import "HomeSegmentV.h"

@interface HomeSegmentV ()

@property (nonatomic, strong) UIButton *selectedBtn;

@end


@implementation HomeSegmentV


- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = UIColor.clearColor;
        [self addViews];
    }
    return self;
}

- (void)addViews {
    
    UIView *backV = [[UIView alloc] init];
    [self addSubview:backV];
    backV.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
    
    NSArray *titleArr = @[@"资产",@"Net"];
    for (int i = 0 ; i < 2; i ++) {
        UIButton *scanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:scanBtn];
        [scanBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [scanBtn setTitleColor:MAIN_BACKCOLOR forState:UIControlStateSelected];
        [scanBtn setTitleColor:TEXT_COLOR_153 forState:UIControlStateNormal];
        [scanBtn addTarget:self action:@selector(scanBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //默认是YES高亮效果
        [scanBtn setAdjustsImageWhenHighlighted:NO];
        [scanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(26 + (60 *i));
            make.top.mas_equalTo(20);
            make.size.mas_equalTo(CGSizeMake(38, 18));
        }];
        scanBtn.tag = 5 + i;
        

        
        if (i == 0) {
            UIView *lineView = [[UIView alloc] init];
            [self addSubview:lineView];
            lineView.backgroundColor = MAIN_ACTIVE_COLOR;
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(scanBtn.mas_bottom).mas_offset(4);
                make.centerX.mas_equalTo(scanBtn.mas_centerX);
                make.height.mas_equalTo(2.5);
                make.width.mas_equalTo(11);
            }];
            GWRadius(lineView, 1);
            lineView.tag = 10 + i;
            scanBtn.selected = YES;
            _selectedBtn = scanBtn;
            
        }
    }
}

- (void)scanBtnClick:(UIButton *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(segmentViewChangeStateWithIndex:)]) {
        [self.delegate segmentViewChangeStateWithIndex:sender.tag];
    }
}

- (void)setHomeModel:(GWHomeModel *)homeModel{
    _homeModel = homeModel;
    UIView *lineV = [self viewWithTag:20];
    UIButton *sender =  [self viewWithTag:homeModel.segmentState + 5];
    
    _selectedBtn.selected = NO;
    sender.selected = YES;
    _selectedBtn = sender;

    [UIView animateWithDuration:12 animations:^{
        [lineV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(sender.mas_bottom).mas_offset(4);
            make.centerX.mas_equalTo(sender.mas_centerX);
            make.height.mas_equalTo(2.5);
            make.width.mas_equalTo(11);
        }];
    }];
}

@end
